﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Proggy.Web.Cms.Cache
{
    /// <summary>
    /// The Url cache
    /// </summary>
    public static class UrlCache
    {
        /// <summary>
        /// The cached node urls - should be set at AppStart / Refresh
        /// </summary>
        public static List<CachedNodeUrl> CachedNodeUrls { get; set; }

        /// <summary>
        /// Returns the url from the url cache
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public static string GetUrlFromNodeId(int nodeId)
        {
            // check cache
            if (Cache.UrlCache.CachedNodeUrls == null)
            {
                throw new Exception("UrlCache.CachedNodeUrls is null. Set this property and try again.");
            }

            // get cached url
            var cachedUrl = Cache.UrlCache.CachedNodeUrls.SingleOrDefault(n => n.NodeId == nodeId);

            // check
            if (cachedUrl == null)
            {
                throw new Exception("CachedUrl object with the id of " + nodeId + " does not exist in the cache.");
            }

            //
            return cachedUrl.NodeUrl;
        }
    }
}
